/**
 * 
 */
package com.java.test.paymanage.mapper;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.paymanage.dto.OnlinePayInfo;


/**
 * @author Administrator
 *
 */
@Component
public class OnlinePayMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<OnlinePayInfo> selectonlinepayList() {

		String sql = "select * from paymanage_onlinepay";

		List<OnlinePayInfo> list = jdbcTemplate.query(sql, new OnlinePayInfo());

		return list;
	}

	public int addonlinepayInfo(OnlinePayInfo info) {

		String sql = "insert into paymanage_onlinepay"
				+ "(open_mode,open_slice,pay_channelname,pay_quota,pay_typename,pay_typevalue,sort,status,terminal,pay_channelvalue)"
				+ "values(?,?,?,?,?,?,?,?,?,?)";

		int i = jdbcTemplate.update(sql, info.getOpenMode(), info.getOpenSlice(), info.getPaychannelName(),
				info.getPayQuota(), info.getPayTypeName(), info.getPayTypeValue(), info.getSort(), info.getStatus(),
				info.getTerminal(),info.getPayChannelValue());

		return i;
	}

	public int updateonlinepayInfo(OnlinePayInfo info) {

		String sql = "update paymanage_onlinepay set open_mode=?,open_slice=?,pay_channelname=?,"
				+ "pay_quota=?,pay_typename=?,pay_typevalue=?,sort=?,status=?,terminal=?,pay_channelvalue=? where id=?";

		int i = jdbcTemplate.update(sql, info.getOpenMode(), info.getOpenSlice(), info.getPaychannelName(),
				info.getPayQuota(), info.getPayTypeName(), info.getPayTypeValue(), info.getSort(), info.getStatus(),
				info.getTerminal(),info.getPayChannelValue(), info.getId());

		return i;
	}
	
	
	public int updatesortonlinepayInfo(List<OnlinePayInfo> info) {

		String sql = "update paymanage_onlinepay set sort=? where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return info.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, info.get(i).getSort());
				ps.setInt(2, info.get(i).getId());
			}
		});

		return 1;
	}
	
	public int updatestatusonlinepayInfo(OnlinePayInfo info) {

		String sql = "update paymanage_onlinepay set status=? where id=?";

		int i = jdbcTemplate.update(sql, info.getStatus(),info.getId());

		return i;
	}
	
	

	public int deleteonlinepayInfo(OnlinePayInfo info) {

		String sql = "delete from paymanage_onlinepay where id=?";

		int i = jdbcTemplate.update(sql, info.getId());

		return i;

	}
	

	
	
}
